package com.mb.mp.controller;

import com.mb.common.controller.BaseController;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import com.mb.mp.domain.MpProductInfoDO;
import com.mb.mp.domain.RechargeCfgDO;
import com.mb.mp.service.IProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author 王源
 * @program mp-parent
 * @description 品牌库未完成
 * @date 2018-09-04 16:44
 **/
@Controller
@RequestMapping("/mp/product")
public class ProductController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private IProductService productService;

    @GetMapping("/index")
    String index(){
        return "/mp/product_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<MpProductInfoDO> list = productService.queryList(query);
        int total = productService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(){
        return "/mp/product_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(MpProductInfoDO productInfo, HttpServletRequest request){
        int result = productService.save(productInfo);
        return R.ok();
    }


    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("productId") Long productId, HttpServletRequest request){
        int result = productService.remove(productId);
        return R.ok();
    }

    @GetMapping("/dtl/{productId}")
    String dtl(@PathVariable("productId") Long productId, Model model){
        model.addAttribute("product",productService.getDtl(productId));
        return "/mp/product_info";
    }


    @GetMapping("/edit/{productId}")
    String edit(@PathVariable("productId") Long productId, Model model){
        model.addAttribute("product",productService.getDtl(productId));
        return "/mp/product_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(MpProductInfoDO productInfo, HttpServletRequest request){
        int result = productService.update(productInfo);
        return R.ok();
    }




    @GetMapping("/rechargeCfg")
    String infoPage(){
        return "/mp/recharge_cfg";
    }

    @ResponseBody
    @GetMapping("/getRecharge/{id}")
    public RechargeCfgDO getRecharge(@PathVariable("id") Long id, Model model){
        RechargeCfgDO rechargeCfgDO = new RechargeCfgDO();
        rechargeCfgDO.setId(id);
        RechargeCfgDO rechargeCfg = productService.getRechargeCfg(rechargeCfgDO);
        return rechargeCfg;
    }


    /**
     * 更新充值配置信息
     * @param request
     * @return
     */
    @PostMapping("/updateRecharge")
    @ResponseBody
    public R updateRecharge(RechargeCfgDO rechargeCfgDO, HttpServletRequest request){
        int result = productService.updateRecharge(rechargeCfgDO);
        return R.ok();
    }





}
